package com.liwang.lanouhn.nvshengo.activities;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.liwang.lanouhn.nvshengo.Info.LikeEvent;
import com.liwang.lanouhn.nvshengo.Info.TutorialInfo;
import com.liwang.lanouhn.nvshengo.R;
import com.liwang.lanouhn.nvshengo.fragments.TutorialFragment;
import com.superplayer.library.SuperPlayer;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by lanouhn on 2016/10/11.
 */
public class TutorialDetailsActivity extends AppCompatActivity implements View.OnClickListener,SuperPlayer.OnNetChangeListener,Serializable {
    private SuperPlayer view_superplayer;
    private ImageView iv_back,iv_line;
    private ImageView iv_share;
    private ImageView iv_newlike;
    private TextView tv_newlike;
    private ImageView iv_newcomment;
    private TextView tv_newcomment;
    private ImageView iv_favourite;
    private TextView tv_favourite;
    private LinearLayout ll_favourite,ll_newlike;
    private RelativeLayout rl_favourite;
    private int times = 0;
    private int tag =0;
    TutorialInfo tutorialInfo;
    private String url;
    private TutorialFragment tutorialFragment;
    private FrameLayout tutorial;
    private android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    private android.support.v4.app.FragmentTransaction transaction;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window window=getWindow();
        window.setFlags(flag,flag);
        setContentView(R.layout.activity_tutorialdetails);
        initView();
        initData();

    }
    private void initView() {
        ll_newlike= (LinearLayout) findViewById(R.id.ll_newlike);
        ll_newlike.setOnClickListener(this);
        tutorial= (FrameLayout) findViewById(R.id.tutorial);
        iv_line= (ImageView) findViewById(R.id.iv_line);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        iv_share = (ImageView) findViewById(R.id.iv_share);
        iv_share.setOnClickListener(this);
        iv_newlike = (ImageView) findViewById(R.id.iv_newlike);
        tv_newlike = (TextView) findViewById(R.id.tv_newlike);
        iv_newcomment = (ImageView) findViewById(R.id.iv_newcomment);
        iv_newcomment.setOnClickListener(this);
        tv_newcomment = (TextView) findViewById(R.id.tv_newcomment);
        iv_favourite = (ImageView) findViewById(R.id.iv_favourite);
        tv_favourite = (TextView) findViewById(R.id.tv_favourite);
        ll_favourite= (LinearLayout) findViewById(R.id.ll_favourite);
        ll_favourite.setOnClickListener(this);
        tutorialInfo = (TutorialInfo) getIntent().getSerializableExtra("info");
        tv_newlike.setText(tutorialInfo.getLike_count()+"");
        tv_newcomment.setText(tutorialInfo.getComment_count()+"");
        url=tutorialInfo.getVideo_url();
        view_superplayer = (SuperPlayer) findViewById(R.id.view_superplayer);
        view_superplayer.setLive(false);//设置该地址的类型,false为视频，true为直播
        view_superplayer.setNetChangeListener(true)//设置监听手机网络的变化
                .setOnNetChangeListener(this)//实现网络变化的回调
                .onPrepared(new SuperPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared() {
                        /**
                         * 监听视频是否已经准备完成开始播放。（可以在这里处理视频封面的显示跟隐藏）
                         */
                    }
                }).onComplete(new Runnable() {
            @Override
            public void run() {
                /**
                 * 监听视频是否已经播放完成了。（可以在这里处理视频播放完成进行的操作）
                 */

            }
        }).onInfo(new SuperPlayer.OnInfoListener() {
            @Override
            public void onInfo(int what, int extra) {
                /**
                 * 监听视频的相关信息。
                 */
            }
        }).onError(new SuperPlayer.OnErrorListener() {
            @Override
            public void onError(int what, int extra) {
                /**
                 * 监听视频播放失败的回调
                 */

            }
        }).setTitle(url)//设置视频的titleName
                .play(url);//开始播放视频
        view_superplayer.setScaleType(SuperPlayer.SCALETYPE_FILLPARENT);//设置视频显示的方式
        view_superplayer.setPlayerWH( 0,
                view_superplayer.getMeasuredHeight() );//设置竖屏的时候屏幕的高度，如果不设置会切换后按照16:9的高度重置

    }
    private void initData() {
        String like = (String) tv_newlike.getText();
        tutorialFragment=TutorialFragment.newInstants(tutorialInfo,like);
        transaction=fm.beginTransaction();
        transaction.add(R.id.tutorial,tutorialFragment);
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.iv_back:
                finish();
            break;
            case R.id.iv_share:
                showShare();

            break;
            case R.id.ll_newlike:
                if (times == 0) {
                    iv_newlike.setBackgroundResource(R.mipmap.ic_liked);
                    tv_newlike.setText((tutorialInfo.getLike_count()+1)+"");
                    times = 1;
                } else {
                    iv_newlike.setBackgroundResource(R.mipmap.ic_new_like);
                    tv_newlike.setText(tutorialInfo.getLike_count()+"");
                    times = 0;
                }
                String like = (String) tv_newlike.getText();
                EventBus.getDefault().post(new LikeEvent(like));
            break;
            case R.id.iv_newcomment:
            break;
            case R.id.ll_favourite:
                if (tv_favourite.getText().equals("收藏")){
                    iv_line.setVisibility(View.VISIBLE);
                    ll_favourite.setBackgroundColor(Color.WHITE);
                    iv_favourite.setBackgroundResource(R.mipmap.btn_good_item_favourite_pressed);
                    tv_favourite.setTextColor(getResources().getColor(R.color.gray));
                    tv_favourite.setText("已收藏");
                    Toast.makeText(TutorialDetailsActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();

                }else {
                    iv_line.setVisibility(View.GONE);
                    ll_favourite.setBackgroundColor(getResources().getColor(R.color.pink));
                    iv_favourite.setBackgroundResource(R.mipmap.btn_good_item_favourite);
                    tv_favourite.setTextColor(Color.WHITE);
                    tv_favourite.setText("收藏");
                    Toast.makeText(TutorialDetailsActivity.this,"取消收藏",Toast.LENGTH_SHORT).show();

                }

            break;
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        view_superplayer.onDestroy();
    }

    /**
     * 网络链接监听类
     */
    @Override
    public void onWifi () {
        Toast.makeText( this, "当前网络环境是WIFI", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onMobile () {
        Toast.makeText( this, "当前网络环境是手机网络", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onDisConnect () {
        Toast.makeText( this, "网络链接断开", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onNoAvailable () {
        Toast.makeText( this, "无网络链接", Toast.LENGTH_SHORT ).show();
    }
    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle(tutorialInfo.getTitle());
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(url);
// text是分享文本，所有平台都需要这个字段
        oks.setText(tutorialInfo.getTitle());
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(tutorialInfo.getAvatar_url());
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("你的私人定制美丽管家");
// site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);

// 启动分享GUI
        oks.show(this);
    }
}
